package com.itsu.integration.service.impl;

import com.alibaba.fastjson.JSON;
import com.itsu.integration.factory.GetCountryRequest;
import com.itsu.integration.factory.ObjectFactory;
import ognl.Ognl;
import ognl.OgnlException;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 构建请求对象
 * @author suben
 * @date 2020/5/1 19:41
 */
@Component
public class PerpareRequest {

    public GetCountryRequest buildRequest(Message message) throws OgnlException {
        GetCountryRequest getCountryRequest = this.buildRequest(JSON.toJSONString(message.getPayload()));
        return getCountryRequest;
    }

    protected GetCountryRequest buildRequest(String payload) throws OgnlException {
        Map map = JSON.parseObject(payload);
        List<String> name = (List) Ognl.getValue("name", map);
        ObjectFactory objectFactory = new ObjectFactory();
        GetCountryRequest getCountryRequest = objectFactory.createGetCountryRequest();
        getCountryRequest.setName(name.get(0));
        return getCountryRequest;
    }

}
